package com.wolfking.jeesite.freemarker.method;

import com.wolfking.jeesite.freemarker.FreemarkerMethod;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@FreemarkerMethod("currentDateFormat")
public class CurrentDateFormatMethod implements TemplateMethodModelEx {

    @Override
    public Object exec(List list) throws TemplateModelException {
        String format = list.get(0).toString();
        long offset = 0;
        if (list.size() > 1) {
            offset = Long.parseLong(list.get(1).toString());
        }
        try {
            return new SimpleDateFormat(format).format(new Date(System.currentTimeMillis() - offset));
        } catch (Exception e) {
            return -1;
        }
    }
}
